<script setup>
import { ref, computed } from 'vue';
import {
  Management, ArrowRight
} from '@element-plus/icons-vue'
import { GetBookAPI, GetstudyDataToDayAPI } from '@/api/studyData.js'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user.js'
const userStore = useUserStore()
const router = useRouter()

// const data = ref({}) // 
// 调用获取今天数据和单词书的接口
const DailyAmount = ref(0) // 	设定的每日单词学习数量。
const TodayAmount = ref(0) // 今天已经学习的数量。
const LearnedAmount = ref(0) // 已学习的数量。
const BookAmount = ref(0) // 单词书的全部单词数量。
const LearnDuration = ref(0)// 今天学习时长。
if (userStore.token) {
  const GetstudyDataSeven = async () => {
    // 同时发起两个接口，需要两个接口的数据      冒号是重命名的意思
    const [{ learn_duration, daily_amount, learned_amount: today_amount }, { learned_amount, book_amount }] = await Promise.all([GetstudyDataToDayAPI(), GetBookAPI()])
    DailyAmount.value = daily_amount
    TodayAmount.value = today_amount
    LearnedAmount.value = learned_amount
    BookAmount.value = book_amount
    LearnDuration.value = Math.floor(learn_duration / 60) // 将秒数转换为分钟数
  }
  GetstudyDataSeven()
}

// 已完成百分比
const finish = computed(() => {
  return (LearnedAmount.value / BookAmount.value).toFixed(2)
})


</script>

<template>
  <div class="page" v-if="!userStore.token">
    <h1>欢迎来到榛榛背单词</h1>
    <div class="text">使用榛榛背单词前请先登录</div>
    <div class="button">
      <el-button class="register" color="#5bbe9e" @click="router.push('/home/redirect')">注册</el-button>
      <el-button class="login" @click="router.push('/home/login')">登录</el-button>
    </div>
  </div>

  <div class="parent" v-else>
    <div class="box">
      <div style="margin:0 0 40px 20% ;"><span style="font-size: 65px; font-weight: bold;">0</span>
        &nbsp;天</div>
      <div class="book">
        <div class="title">
          <div class="titleLeft">四级词汇</div>
          <div class="titleRight" @click="router.push('/home/words')">查看此表</div>
        </div>
        <div class="set" @click="router.push('/home/setting/learn')">
          <el-icon size="20">
            <Management />
          </el-icon><span style="padding: 0 5px;cursor: pointer;">学习设置</span>
          <el-icon size="20">
            <ArrowRight />
          </el-icon>
        </div>
        <div class="title">
          <div class="left">已完成 <span>{{ finish }}</span>%</div>
          <div class="right"><span>{{ LearnedAmount }}</span> / <span>{{ BookAmount }}</span> 词</div>
        </div>
        <!-- 进度条 -->
        <el-progress :percentage="finish" color="#5BBE9E" :show-text="false" />
        <div class="data">
          <div class="item">
            <div style="color: #666666;">应学</div>
            <div style="font-size: 20px; font-weight: bold;">{{ DailyAmount }}</div>
          </div>
          <div class="item">
            <div style="color: #666666;">已学</div>
            <div style="font-size: 20px; font-weight: bold;">{{ TodayAmount }}</div>
          </div>
          <div class="item">
            <div style="color: #666666;">时长</div>
            <div style="font-size: 20px; font-weight: bold;">{{ LearnDuration }}<span>min</span></div>
          </div>
        </div>
      </div>
      <el-button size="large" type="success" class="button" @click="router.push('/home/task')">开始学习</el-button>

    </div>
  </div>

</template>

<style lang="scss" scoped>
.page {
  width: 560px;
  height: 250px;
  margin: 0 auto;

  h1 {
    margin: 50px 0 20px 0;
  }

  .text {
    margin-bottom: 60px;
  }

  .button {
    display: flex;
    justify-content: space-around;

    .register {
      border-radius: 20px;
      flex: 1;
      color: #fff;
    }

    .login {
      border-radius: 20px;
      flex: 1;

    }
  }

}

.parent {
  width: 100%;
  height: 90vh;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(11, 1fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;

  .box {

    grid-area: 2 / 3 / 11 / 9;

    .book {
      width: 100%;
      height: 235px;
      background: #DDDDDD;
      border-radius: 16px;
      padding: 20px;
      max-width: 520px;
      margin: 0 auto;

      .title {

        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;
        color: #666666;

        .titleLeft {
          font-size: 25px;
          font-weight: bold;
          color: #000000;
        }

        .titleRight {
          color: #5bbe9e;
          font-weight: 600;
          cursor: pointer;
        }

        .left {
          font-weight: bold;
        }

        .right {
          font-weight: bold;
        }
      }
    }

    .set {
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      color: #555555;
    }

    .button {
      display: block;
      width: 50%;
      height: 45px;
      background: #5bbe9e;
      border-radius: 30px;
      margin: 40px auto;
    }
  }

  .data {
    display: flex;
    justify-content: space-around;
    margin-top: 10px;

    .item {
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
}
</style>